package org.ichow.utils
{

	public class DataUtils
	{
		public function DataUtils()
		{
		}
		
		//-------------------------
		// 转换 Object 为 as 代码字符 
		//-------------------------
		/**
		 * 转换 Object 为 as 代码字符 
		 * @param source
		 * @return 
		 * 
		 */		
		public static function convertObjectToASString(source:Object,childSign:String="children"):String
		{
			if(childSign!="children")
			{
				//filter(source,childSign);
			}
			return convert(source,true,true);
		}
		
		/**
		 * 转换题目 
		 * @param q
		 * @return 
		 */		
		public static function convert(o:Object,
			senl:Boolean = false,attnl:Boolean = false,tab:String=""):String
		{
			var str:String = "";
			switch(typeof(o))
			{
				case "object":
					if(o is Array)
					{
						str = convertArray(o,senl,attnl,tab+"\t");
					}else
					{
						str = convertObject(o,senl,attnl,tab+"\t");
					}
					break;	
				case "string":
					str = "\""+o.toString()+"\"";
					break;	
				
				default:
					str = o.toString();
					break;
			}
			return str;
		}
		
		public static function convertArray(o:Object,
			senl:Boolean = false,attnl:Boolean = false,tab:String=""):String
		{
			var arr:Array = o as Array;
			var str:String = (senl ? "\n" : "")+(senl ? tab : "")+"["+(senl ? "\n" : "");
			var len:int = arr.length;
			
			for(var i:int=0;i<len;i++)
			{
				//题目需要换行
				str += (attnl?tab+"\t":"")+convert(arr[i],(typeof(arr[i])=="object" && arr[i]["ext"]=="Q"),(typeof(arr[i])=="object" && arr[i]["ext"]=="Q"),tab+"\t");
				if(i!=len-1)
				{
					str+=","+(attnl ? "\n" : "");
				}
			}
			str +=(senl ? "\n" : "")+(senl ? tab : "")+"]";
			return str;
		}
		
		public static function convertObject(o:Object,
			senl:Boolean = false,attnl:Boolean = false,tab:String=""):String
		{
			var add:Boolean = false;
			var str:String = (senl ? "\n" : "")+(senl ? tab : "")+"{"+(senl ? "\n" : "");
			for(var i:String in o)
			{
				if(add)str+=","+(attnl ? "\n" : "");
				//data 属性需要换行
				str+=(attnl?tab+"\t":"")+i+":"+convert(o[i],(i == "data"),(i == "data"),(i == "data") ? tab+"\t":"");
				add = true;
			}
			str+=(senl ? "\n" : "")+(senl ? tab : "")+"}";
			return str;
		}
		
		public static function filter(o:Object,ssc:String="children",cs:String="data"):void
		{
			for(var i:String in o)
			{
				if(i == ssc)
				{
					if(o[ssc].length == 0)
					{
						delete o[ssc];
						continue;
					}
				}
				
				if(typeof(o[i]) == "object")
				{
					filter(o[i],ssc,cs);
				}
				
				if(i==ssc)
				{
					o[cs] = o[ssc];
					o[ssc] = null;
					delete o[ssc];
				}
			}
		}
		
		
		//---------------------
		//Object to XML
		//---------------------
		/**
		 * 字符转换成对象 
		 * @param source
		 * @param childSign
		 * @return 
		 * 
		 */		
		public static function convertObjectToXml(source:Object):XML
		{
			var xml:XML = createItem(source);
			return xml;
		}
		
		/**
		 * Object to XML 
		 * @param obj
		 * @return 
		 * 
		 */		
		private static function createItem(obj:Object):XML
		{
			var item:XML = <item/>;
			var children:Array;
			for(var i:String in obj)
			{
				if(i!="children")
				{
					item.@[i] = obj[i];
				}else
				{
					children = obj[i];
					for(var j:int=0;j<children.length;j++)
					{
						item.appendChild(createItem(children[j]));
					}
				}
			}
			return item;
		}
		
		
		//---------------------
		//XML to Object
		//---------------------
		/**
		 * XML to Object 
		 * @param value XML数据 
		 * @return 
		 * 
		 */		
		public static function convertXMLToObject(value:Object):Object
		{
			var tempData:Object={id:"000000",children:[],des:"知识点"};
			var itemObj:Object;
			var playlistXML:XML = new XML(value);
			var itemList:XMLList = playlistXML.item;
			
			for(var i:int=0;i<itemList.length();i++)
			{
				itemObj = analyseItem(itemList[i]);
				tempData.children.push(itemObj);
			}
			filter(tempData,"children","data");
			return tempData;
		}
		
		private static function analyseItem(itemXML:XML):Object
		{
			var itemObj:Object={};
			itemObj.children=[];
			var attXML:XML;
			var attName:String;
			for(var j:int=0;j<itemXML.attributes().length();j++)
			{
				attXML = itemXML.attributes()[j];
				attName = attXML.name().toString();
				itemObj[attName] = attName != "answer" ? attXML.toString() : attXML.toString().split(",");;
			}
			if(itemXML.hasComplexContent())
			{
				for(j=0;j<itemXML.children().length();j++)
				{
					itemObj.children.push(analyseItem(itemXML.children()[j]));
				}
			}
			return itemObj;
		}
	}
}